﻿using Bricks.Caching;
using Bricks.Injecting;
using System;

namespace Motar.Web.Mvc
{

    /// <summary>
    /// 
    /// </summary>
    public class LocalizeAttribute : LocalizeAttributeBase
    {

        /// <summary>
        /// 
        /// </summary>
        private readonly IValueResolver<ICacheFacade> _cacheFacadeResolver;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cacheFacadeResolver"></param>
        public LocalizeAttribute(IValueResolver<ICacheFacade> cacheFacadeResolver)
        {
            if (cacheFacadeResolver == null)
            {
                throw new ArgumentNullException("cacheFacadeResolver");
            }
            _cacheFacadeResolver = cacheFacadeResolver;
        }

        /// <summary>
        /// 
        /// </summary>
        public LocalizeAttribute() :
            this(new SingleValueResolver<ICacheFacade, NullCacheFacade>())
        {
        }

        /// <summary>
        /// 
        /// </summary>
        protected IValueResolver<ICacheFacade> CacheFacadeResolver
        {
            get { return _cacheFacadeResolver; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="locale"></param>
        protected override void Save(string locale)
        {
            if (string.IsNullOrEmpty(locale))
            {
                throw new ArgumentNullException("locale");
            }

            CacheFacadeResolver.Resolve().Add(typeof(Localization).FullName, locale);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override string Load()
        {
            return CacheFacadeResolver.Resolve().Get(typeof(Localization).FullName).ToString();
        }
    }
}
